package com.kabuda.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kabuda.blog.dao.mapper.ArticleMapper;
import com.kabuda.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author kabuda
 * @create 2023-01-18 13:31
 */
@Component
public class ThreadService {
    @Async("taskExecutor")
    public void UpdateViewCount(ArticleMapper articleMapper,Article article) {
            try {
                //模拟阻塞五秒
                Thread.sleep(5000);
                System.out.println("更新文章浏览量");

                //设置更新字段
                Article updateArticle = new Article();
                updateArticle.setViewCounts(article.getViewCounts()+1);

                LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
                //根据id更新
                queryWrapper.eq(Article::getId,article.getId());
                //乐观锁 version版本号
                //update article set viewCounts = article.getViewCounts() + 1 where view_counts=article.getViewCounts and article_d=article.getId()
                queryWrapper.eq(Article::getViewCounts,article.getViewCounts());

                articleMapper.update(updateArticle,queryWrapper);
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }

    }
}
